package com.example.newsdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class ViewNewsActivity extends AppCompatActivity {
    private String newsUrl;
    private String newsID;
    private WebView webView;
    private Button btnComment;
    private Button btnMoreComment;
    private EditText edtComment;
    private Button btnCollect;
    private boolean isCollect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_news);

        newsUrl = getIntent().getStringExtra("NEWS_URL");
        newsID = getIntent().getStringExtra("NEWS_ID");

        webView = findViewById(R.id.wv_news);
        webView.loadUrl(newsUrl);

        initMoreCommentViews();
        initCommentViews();
        initCollectButtonz();
        updataCollectState();

    }

    private void initMoreCommentViews() {
        btnMoreComment = findViewById(R.id.btn_morecomment);
        btnMoreComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ViewNewsActivity.this,ViewMoreCommentsActivity.class);
                intent.putExtra("NEWSID",newsID);
                startActivity(intent);
            }
        });
    }

    private void updataCollectState() {
        if (BmobUser.getCurrentUser() == null) {
            return;
        }

        String userID = BmobUser.getCurrentUser().getObjectId();
        BmobQuery<Collect> query = new BmobQuery<>();
        query.addWhereEqualTo("new_id",newsID);
        query.addWhereEqualTo("user_id",userID);
        query.findObjects(new FindListener<Collect>() {
            @Override
            public void done(List<Collect> list, BmobException e) {
                if (list != null && list.size() >0) {
                    isCollect = list.get(0).getIs_collect();
                    if (isCollect) {
                        btnCollect.setText("取消收藏");
                    }else {
                        btnCollect.setText("收藏");
                    }
                }
            }
        });
    }

    private void initCollectButtonz() {
        btnCollect = findViewById(R.id.btn_collect);
        btnCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (BmobUser.getCurrentUser() == null) {
                    Intent intent = new Intent();
                    intent.setClass(ViewNewsActivity.this,LoginActivity.class);
                    startActivity(intent);
                }
                final String userID = BmobUser.getCurrentUser().getObjectId();
                BmobQuery<Collect> query = new BmobQuery<>();
                query.addWhereEqualTo("new_id",newsID);
                query.addWhereEqualTo("user_id",userID);
                query.findObjects(new FindListener<Collect>() {
                    @Override
                    public void done(List<Collect> list, BmobException e) {
                        if (list != null && list.size()>0) {
                            Collect collect = new Collect();
                            collect.setIs_collect(!isCollect);
                            collect.update(list.get(0).getObjectId(), new UpdateListener() {
                                @Override
                                public void done(BmobException e) {
                                    refreshCollectButton();

                                }
                            });
                        } else {
                            Collect collect = new Collect();
                            collect.setNew_id(newsID);
                            collect.setUser_id(userID);
                            collect.setIs_collect(!isCollect);
                            collect.save(new SaveListener<String>() {
                                @Override
                                public void done(String s, BmobException e) {
                                    refreshCollectButton();
                                }
                            });
                        }
                    }
                });
            }
        });
    }

    private void refreshCollectButton() {
        isCollect = !isCollect;
        if (isCollect) {
            btnCollect.setText("取消收藏");
        }else {
            btnCollect.setText("收藏");
        }
    }

    private void initCommentViews() {
        edtComment = findViewById(R.id.edt_comment);
        btnComment = findViewById(R.id.btn_comment);
        btnComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(BmobUser.getCurrentUser() == null){
                    Intent intent = new Intent();
                    intent.setClass(ViewNewsActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                String content = edtComment.getText().toString();
                String userID = BmobUser.getCurrentUser().getObjectId();
                Comment comment = new Comment();
                comment.setNews_id(newsID);
                comment.setUser_id(userID);
                comment.setIs_del(false);
                comment.setContent(content);
                comment.save(new SaveListener<String>() {
                    @Override
                    public void done(String s, BmobException e) {
                        Log.e("ViewNewsActivity","评论发表成功");
                        Toast.makeText(ViewNewsActivity.this,"评论发表成功", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });
    }
}
